from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from goods.models import GoodsChannel, GoodsChannelGroup, GoodsCategory
from meiduo_admin.serializers.channel import ChannelSerializer, GoodsChannelGroupSerializer, GoodsCategorySerializer
from meiduo_admin.utils import PageNum


class ChannelView(ModelViewSet):
    # 指定序列化器
    serializer_class = ChannelSerializer
    # 指定查询集
    queryset = GoodsChannel.objects.all()
    # 指定分页器
    pagination_class = PageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    def channel_types(self, request):
        groups = GoodsChannelGroup.objects.all()
        ser = GoodsChannelGroupSerializer(groups, many=True)
        return Response(ser.data)

    def categories(self, request):
        categories = GoodsCategory.objects.filter(subs=None)
        ser = GoodsCategorySerializer(categories, many=True)
        return Response(ser.data)
